package com.java8.lambda;

import com.java8.lambda.bean.Employee;
import com.java8.lambda.impl.FilterEmployeeByAge;
import com.java8.lambda.impl.FilterEmployeeByMoney;
import com.java8.lambda.impl.impl.MyPredicate;
import org.junit.Test;

import java.util.*;

public class TestLambda {

    //原来的匿名内部类
    @Test
    public void test1() {
        Comparator<Integer> comparable = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };

        //传入参数
        TreeSet<Integer> treeSet = new TreeSet(comparable);
    }


    //Lambda表达式
    @Test
    public void test2() {
        Comparator<Integer> comparable = (x,  y) -> Integer.compare(x, y);

        //传入参数
        TreeSet<Integer> treeSet = new TreeSet(comparable);
    }


    List<Employee> employees = Arrays.asList(
            new Employee("张三",18,9999.99),
            new Employee("李四",38,5555.55),
            new Employee("王五",50,6666.99),
            new Employee("赵六",16,8888.99),
            new Employee("田七",8,7777.99)

    );

    //需求: 获取当前公司中员工年龄大于35的员工信息
    @Test
    public void test3() {
        List<Employee> employees = filterEmployees(this.employees);

        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }



    public List<Employee> filterEmployees(List<Employee> list) {
        List<Employee> arrayList = new ArrayList<>();

        for(Employee employee : list) {
            if(employee.getAge() >= 35) {
                arrayList.add(employee);
            }
        }
        return arrayList;
    }

    //需求:获取当前公司中员工工资大于5000的员工信息
    public List<Employee> filterEmployee2(List<Employee> list) {
        List<Employee> arrayList = new ArrayList<>();

        for(Employee employee : list) {
            //只在这里修改，会产生大量冗余代码，必须解决
            if(employee.getMoney() >= 5000) {
                arrayList.add(employee);
            }
        }
        return arrayList;
    }

    //优化方式一:
    @Test
    public void test4() {
        //根据年龄过滤
        List<Employee> employees = filterEmployee(this.employees, new FilterEmployeeByAge());

        for (Employee employee : employees) {
            System.out.println(employee);
        }

        //根据工资过滤
        List<Employee> employees1 = filterEmployee(this.employees, new FilterEmployeeByMoney());

        for (Employee employee : employees1) {
            System.out.println(employee);
        }


    }

    public List<Employee> filterEmployee(List<Employee> list, MyPredicate<Employee> myPredicate) {
        List<Employee> employees = new ArrayList<>();

        for(Employee employee : list) {
            if(myPredicate.test(employee)) {
                employees.add(employee);
            }
        }
        return employees;
    }

    //优化方式二:匿名内部类
    @Test
    public void test5() {
        List<Employee> employees = filterEmployee(this.employees, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getAge() >= 35;
            }
        });
        for (Employee employee : employees) {
            System.out.println(employee);
        }

    }

    //优化方式三:Lambda表达式
    @Test
    public void test6() {
        List<Employee> employees = filterEmployee(this.employees, e -> e.getAge() >= 35);
        employees.forEach(System.out::println);
    }


    //优化方式四:
    @Test
    public void test7() {
        this.employees.stream()
                .filter((e) -> e.getMoney() >= 5000)
                .limit(2)
                .forEach(System.out::println);

        System.out.println("--------------------------------------------------");

        employees.stream().map(Employee::getName).forEach(System.out::println);
    }

}
